package com.yunc.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.utils.R;
import com.yunc.modules.sys.entity.SysRole;
import com.yunc.modules.sys.service.ISysRoleService;
import com.yunc.modules.sys.service.ISysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统角色
 *
 * @author congcong tao
 * @date 2018/5/14 16:14
 */
@RestController
@RequestMapping("sysRole")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysRoleController {

    private final ISysRoleService sysRoleService;
    private final ISysUserRoleService sysUserRoleService;

    @GetMapping("/list")
    public R list(String userId) {
        QueryWrapper<SysRole> ew = new QueryWrapper<>(new SysRole());
        List<SysRole> sysRoleList = sysRoleService.list(ew);
        // 当前用户拥有的角色
        // roleId
        List<String> resultList = sysRoleList.stream().map(SysRole::getId).collect(Collectors.toList());
        List<String> userRoleList;
        if (userId != null) {
            userRoleList = sysUserRoleService.listRoleList(userId);
        } else {
            userRoleList = new ArrayList<>();
        }
        return R.ok().put("sysRoleList", sysRoleList).put("sysRoleIdList", resultList).put("userRoleList", userRoleList);
    }

    @GetMapping("/page")
    public R page(Page<SysRole> page, SysRole sysRole) {
        QueryWrapper<SysRole> ew = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysRole.getStatus())) {
            ew.eq("status", sysRole.getStatus());
        }
        if (StringUtils.isNotEmpty(sysRole.getRoleName())) {
            ew.like("role_name", sysRole.getRoleName());
        }
        ew.orderByAsc("order_num").orderByDesc("create_time");
        sysRoleService.page(page, ew);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    /**
     * 菜单信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable String id) {
        SysRole role = sysRoleService.getById(id);
        return R.ok().put("role", role);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @CacheEvict(value = {"sysUserRoleMenus"}, allEntries = true)
    public R save(SysRole role) {
        return sysRoleService.saveRole(role);
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    @CacheEvict(value = {"sysUserRoleMenus"}, allEntries = true)
    public R update(SysRole role) {
        return sysRoleService.updateRole(role);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{roleId}")
    @CacheEvict(value = {"sysUserRoleMenus"}, allEntries = true)
    public R delete(@PathVariable String roleId) {
        return sysRoleService.deleteRole(roleId);
    }

    @RequestMapping("export")
    public void export(HttpServletResponse response, SysRole sysRole) {
        QueryWrapper<SysRole> ew = new QueryWrapper<>(sysRole);
        List<SysRole> sysRoleList = sysRoleService.list(ew);
        // 导出操作
        // EasyPoiUtil.exportExcel(sysRoleList, "角色", "角色", SysRole.class, "角色.xls", response);
    }
}
